package com.alpha.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.alpha.bom.Exam;
import com.alpha.bom.PracticeExam;
import com.alpha.bom.Professor;
import com.alpha.bom.RealExam;
import com.alpha.dao.ExamDao;

/**
 * Servlet implementation class CreateExamServlet
 */
@WebServlet("/CreateExamServlet")
@ServletSecurity(@HttpConstraint(rolesAllowed = {"admin"}))
public class CreateExamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(CreateExamServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateExamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		logger.info(this.getClass().getSimpleName()+" post method started.");		
		
		Professor professor = null;
		Exam exam = null;
		String examName = "";
		String examCategory = "";
		String examType = "";
		
		HttpSession session = request.getSession();
		
		if(session.getAttribute("user")!=null && (session.getAttribute("user") instanceof Professor)){
			professor = (Professor)session.getAttribute("user");
		} else {
			response.sendRedirect("Login.jsp?message=Access+denied.");
			return;
		}
		
		if(request.getParameter("examName")!=null && request.getParameter("examCategory")!=null && request.getParameter("examType")!=null){
			examName = request.getParameter("examName");
			examCategory = request.getParameter("examCategory");
			examType = request.getParameter("examType");
		} else {
			response.sendRedirect("ExamsAdminServlet?error=Invalid+action.");
			return;
		}
		
		if(examType.equals("RealExam")){
			exam = new RealExam(0,examName);
			((RealExam)exam).setLocked(true);
			((RealExam)exam).setNumberOfStudents(0);
		} else {
			exam = new PracticeExam(0,examName);
		}
		
		if(exam!=null){
			exam.setProfessorId(professor.getUserId());
			exam.setCategory(examCategory);
			ExamDao eDao = new ExamDao();
			try {
				eDao.insertExam(exam);
				response.sendRedirect("ExamsAdminServlet?success=Exam+created!");
				return;
			} catch (Exception e) {
				logger.error(e.getMessage());
				response.sendRedirect("ExamsAdminServlet?error=Exam+creation+failed.");
				return;
			}
		}
	}

}
